global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_A18_keywords_prevalence.log, replace name(tabl)

capture noi {
    
    * Statistics on the classification at the technological category level
    texdoc init ${tab_dir}/appendix/Table_A18_keywords_prevalence.tex, replace
    texdoc stlog, nolog

    * get statistics
    foreach class in ipc6XX ipc4 ipc4_pairs {
        import delim ${classification_dir}/V6/codes_auto95_`class'_n.csv, varnames(1) clear
        keep share* 
        rename share_* *
        rename share any
        foreach comp in any robot automat cnc labor { 
                qui sum `comp', d
                foreach stat in mean sd p25 p50 p75 p90 p95 p99 { 
                    sleep 800
                    texdoc local `class'_`comp'_`stat' : display %9.2f r(`stat')
            }
        }
    }

    * output table (via texdoc)
    texdoc local notes "This table computes summary statistics on the share of patents with any automation keywords, robot keywords, automat* keywords, CNC keywords or labor keywords for each type of technological categories (6-digit C/IPC codes, pairs of 4-digit C/IPC codes and combinations of 4-digit C/IPC codes with G05 or G06) within machinery with at least 100 patents."
    sleep 1000
    texdoc stlog close
    qui include ${code_dir}/config/texdoctools.do
    tex `texpreamble'

    /*tex
    \begin{tabular}{@{}p{0.035\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering\arraybackslash}p{0.04\textwidth}@{}}
    \toprule
    & \multicolumn{5}{c}{C/IPC6} & \multicolumn{5}{c}{C/IPC4 + (G05 or G06)} & \multicolumn{5}{c}{C/IPC4 pairs} \\
    \cmidrule(lr){2-6}\cmidrule(lr){7-11}\cmidrule(lr){12-16}  & All & Robot & Automat* & CNC & Labor & All & Robot & Automat* & CNC & Labor & All & Robot & Automat* & CNC & Labor \\
    \midrule
    Mean & `ipc6XX_any_mean' & `ipc6XX_robot_mean' & `ipc6XX_automat_mean' & `ipc6XX_cnc_mean' & `ipc6XX_labor_mean' & `ipc4_any_mean' & `ipc4_robot_mean' & `ipc4_automat_mean' & `ipc4_cnc_mean' & `ipc4_labor_mean' & `ipc4_pairs_any_mean' & `ipc4_pairs_robot_mean' & `ipc4_pairs_automat_mean' & `ipc4_pairs_cnc_mean' & `ipc4_pairs_cnc_mean' \\
    SD   & `ipc6XX_any_sd' & `ipc6XX_robot_sd' & `ipc6XX_automat_sd' & `ipc6XX_cnc_sd' & `ipc6XX_labor_sd' & `ipc4_any_sd' & `ipc4_robot_sd' & `ipc4_automat_sd' & `ipc4_cnc_sd' & `ipc4_labor_sd' & `ipc4_pairs_any_sd' & `ipc4_pairs_robot_sd' & `ipc4_pairs_automat_sd' & `ipc4_pairs_cnc_sd' & `ipc4_pairs_cnc_sd' \\
    25th  & `ipc6XX_any_p25' & `ipc6XX_robot_p25' & `ipc6XX_automat_p25' & `ipc6XX_cnc_p25' & `ipc6XX_labor_p25' & `ipc4_any_p25' & `ipc4_robot_p25' & `ipc4_automat_p25' & `ipc4_cnc_p25' & `ipc4_labor_p25' & `ipc4_pairs_any_p25' & `ipc4_pairs_robot_p25' & `ipc4_pairs_automat_p25' & `ipc4_pairs_cnc_p25' & `ipc4_pairs_cnc_p25' \\
    50th  & `ipc6XX_any_p50' & `ipc6XX_robot_p50' & `ipc6XX_automat_p50' & `ipc6XX_cnc_p50' & `ipc6XX_labor_p50' & `ipc4_any_p50' & `ipc4_robot_p50' & `ipc4_automat_p50' & `ipc4_cnc_p50' & `ipc4_labor_p50' & `ipc4_pairs_any_p50' & `ipc4_pairs_robot_p50' & `ipc4_pairs_automat_p50' & `ipc4_pairs_cnc_p50' & `ipc4_pairs_cnc_p50' \\
    75th & `ipc6XX_any_p75' & `ipc6XX_robot_p75' & `ipc6XX_automat_p75' & `ipc6XX_cnc_p75' & `ipc6XX_labor_p75' & `ipc4_any_p75' & `ipc4_robot_p75' & `ipc4_automat_p75' & `ipc4_cnc_p75' & `ipc4_labor_p75' & `ipc4_pairs_any_p75' & `ipc4_pairs_robot_p75' & `ipc4_pairs_automat_p75' & `ipc4_pairs_cnc_p75' & `ipc4_pairs_cnc_p75' \\
    90th  & `ipc6XX_any_p90' & `ipc6XX_robot_p90' & `ipc6XX_automat_p90' & `ipc6XX_cnc_p90' & `ipc6XX_labor_p90' & `ipc4_any_p90' & `ipc4_robot_p90' & `ipc4_automat_p90' & `ipc4_cnc_p90' & `ipc4_labor_p90' & `ipc4_pairs_any_p90' & `ipc4_pairs_robot_p90' & `ipc4_pairs_automat_p90' & `ipc4_pairs_cnc_p90' & `ipc4_pairs_cnc_p90' \\
    95th  & `ipc6XX_any_p95' & `ipc6XX_robot_p95' & `ipc6XX_automat_p95' & `ipc6XX_cnc_p95' & `ipc6XX_labor_p95' & `ipc4_any_p95' & `ipc4_robot_p95' & `ipc4_automat_p95' & `ipc4_cnc_p95' & `ipc4_labor_p95' & `ipc4_pairs_any_p95' & `ipc4_pairs_robot_p95' & `ipc4_pairs_automat_p95' & `ipc4_pairs_cnc_p95' & `ipc4_pairs_cnc_p95' \\
    995th  & `ipc6XX_any_p99' & `ipc6XX_robot_p99' & `ipc6XX_automat_p99' & `ipc6XX_cnc_p99' & `ipc6XX_labor_p99' & `ipc4_any_p99' & `ipc4_robot_p99' & `ipc4_automat_p99' & `ipc4_cnc_p99' & `ipc4_labor_p99' & `ipc4_pairs_any_p99' & `ipc4_pairs_robot_p99' & `ipc4_pairs_automat_p99' & `ipc4_pairs_cnc_p99' & `ipc4_pairs_cnc_p99' \\
    \bottomrule
    \end{tabular}
    tex*/
    tex `texpostamble'
    texdoc close

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close tabl